package com.lanou3g.zjl.chating.home.chatwindow.child;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMMessage;
import com.lanou3g.zjl.chating.R;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by dllo on 15/11/23.
 */
public class ChatWindowFrgRv extends RelativeLayout {
    private RecyclerView cwfRv_recyclerView;
    private Context context;
    private ChatWindowFrgRvAdapter chatWindowFrgRvAdapter;
    private String username;
    private EMConversation conversation;
    private List<EMMessage> list;
    private int chatType;
//    private String picturePath;

    static final int MSG_UPDATE_LIST = 0;

    public ChatWindowFrgRv(Context context) {
        super(context);
        initView(context);
    }

    public ChatWindowFrgRv(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ChatWindowFrgRv(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.chatwindowfrg_rv, this);
        cwfRv_recyclerView = (RecyclerView) findViewById(R.id.cwfRv_recyclerView);
    }

    public void initView(Context c, String username, int chatType) {
        this.chatType = chatType;
        this.username = username;
        this.context = c;
//        this.picturePath = picturePath;

        // 加载 聊天消息列表 布局
        chatWindowFrgRvAdapter = new ChatWindowFrgRvAdapter(getContext());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 1);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        cwfRv_recyclerView.setLayoutManager(gridLayoutManager);
        cwfRv_recyclerView.setAdapter(chatWindowFrgRvAdapter);


        conversation = EMChatManager.getInstance().getConversation(username);
        conversation.markAllMessagesAsRead();

        list = conversation.getAllMessages();
        chatWindowFrgRvAdapter.addData(list);
        cwfRv_recyclerView.smoothScrollToPosition(chatWindowFrgRvAdapter.getItemCount());

    }

    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_UPDATE_LIST:
                    if (chatWindowFrgRvAdapter != null)
                        chatWindowFrgRvAdapter.notifyDataSetChanged();
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };

    public void refresh() {
        Message msg = handler.obtainMessage(MSG_UPDATE_LIST);
        handler.sendMessage(msg);
    }
}
